package com.windbell;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;

import com.windbell.layout.FlipperLayout;
import com.windbell.ui.Desktop;
import com.windbell.ui.NewsFeed;
import com.windbell.ui.NewsFeed2;
/**
 * ��ҳ
 * @author Administrator
 *
 */
public class DesktopActivity extends Activity {
	private Desktop mDesktop;
	private NewsFeed newsFeed;
	private NewsFeed2 newsFeed2;
	FlipperLayout mRoot;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mRoot = new FlipperLayout(DesktopActivity.this);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mRoot.setLayoutParams(params);
		mDesktop = new Desktop(getApplication(), this);
		newsFeed = new NewsFeed(getApplication(), this);
		newsFeed2 = new NewsFeed2(getApplication(), this);
			
		
		mRoot.addView(mDesktop.getView(), params);
		mRoot.addView(newsFeed.getView(), params);
		setContentView(mRoot);
		setListener();	
	}
	public void setListener(){
		mDesktop.setOnChangeViewListener(new Desktop.onChangeViewListener() {
			@Override
			public void onChangeView(int arg0) {
				if(arg0==0){
					mRoot.close(newsFeed2.getView());
				}else{
					mRoot.close(newsFeed.getView());
				}
			}
		});
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			 if (mRoot.getScreenState() == FlipperLayout.SCREEN_STATE_CLOSE) {
				 mRoot.open();
			 } else {
			dialog();
			 }
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void dialog() {
		AlertDialog.Builder builder = new Builder(DesktopActivity.this);
		builder.setMessage("确定退出程序?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				finish();
				android.os.Process.killProcess(android.os.Process.myPid());
				System.exit(0);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		builder.create().show();
	}

	public void open() {
		// if (mRoot.getScreenState() == FlipperLayout.SCREEN_STATE_CLOSE) {
		// mRoot.open();
		// }
	}
}
